#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1e5 + 10;
typedef long long ll;
ll a[N];
int n;
int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    sort(a + 1, a + 1 + n);
    ll ret = 0;
    /*for(int i=1;i<=n;i++)
        {
            ret+=abs(a[i]-a[(n+1)/2]);
        }*/
    for (int i = 1; i <= n / 2; i++)
    {
        ret += abs(a[i] - a[n + 1 - i]);
    }
    cout << ret;
    return 0;
}